
/*
 * Suningmatch
 * created by 2016-07-14
 */

;
var Suningmatch = Suningmatch || {};

Suningmatch.appId = "wx6aed9fdd44da794e";
//根据环境更换登录服务器地址
if (location.host.indexOf("dj.suning.com") != -1) {
	Suningmatch.wxLogin = "http://mobts.suning.com/suning-web-mobile/weixin/public/requestUserInfo.htm?weixinRedirectUrl=";
} else {
	Suningmatch.wxLogin = "http://mobimsgpre.cnsuning.com/suning-web-mobile/weixin/public/requestUserInfo.htm?weixinRedirectUrl=";
}


Suningmatch.DI = {
    getTopic: "/sawp/contend/wxTopic/index.htm",
    soaTopic: "/sawp/contend/soaTopic/index.do",
    getLoginInfo: "/sawp/contend/soaTopic/loginInfo.do",
    getTopicforOA: "/sawp/contend/soaTopic/query.do",
    getComm: "/sawp/contend/soaTopic/queryCommList.do",
    WXgetComm: "/sawp/contend/wxTopic/queryCommList.htm",
    queryAllTopic: "/sawp/contend/queryAllTopic.htm",
    commVote: "/sawp/contend/commVote.htm", //踩 赞
    topicVote: "/sawp/contend/soaTopic/topicVote.do", //同意 反对
    WXtopicVote: "/sawp/contend/wxTopic/topicVote.htm", //同意 反对
    whiteComm: "/sawp/contend/soaTopic/comment.do", //发表评论
    WXwhiteComm: "/sawp/contend/wxTopic/comment.htm", //发表评论
    getNmInfo: "/sawp/contend/getNmInfo.htm", //获取匿名信息
    delComm: "/sawp/contend/soaTopic/delComm.do",
    WXdelComm: "/sawp/contend/wxTopic/delComm.htm",
    uploadMisc: "/sawp/contend/uploadMisc.htm",
    modifyUserHeadPic: "/sawp/contend/soaTopic/modifyUserHeadPic.do",
    getjsapiconfig: "/qing-web/weixin/getjsapiconfig.do",
    uploadImg: "http://uimgpre.cnsuning.com/"
};

Suningmatch.wxReady = function(e) {
    $.ajax({
        type: "post",
        url: Suningmatch.DI.getjsapiconfig,
        data: {
            appId: Suningmatch.appId,
            url: encodeURIComponent(location.href)
        },
        dataType: "json",
        success: function(t) {
            wx.config({
                    appId: Suningmatch.appId,
                    timestamp: t.data.timestamp,
                    nonceStr: t.data.nonceStr,
                    signature: t.data.signature,
                    jsApiList: ["onMenuShareTimeline", "onMenuShareAppMessage", "chooseImage", "previewImage", "uploadImage", "downloadImage"]
                }),
                wx.ready(function() {
                    Suningmatch.isConfig = !0;
                    e && Suningmatch.setShare(e);
                }),
                wx.error(function(e) {
                    console.error("share error:" + e)
                })
        },
        error: function(e) {
            console.log(e)
        }
    })
};
Suningmatch.shareInfo = {
    share_img: "",
    share_desc: "",
    share_title: "",
    share_link: ""
};
Suningmatch.wxShare = function(e, t) {
    var n = Suningmatch.shareInfo.share_link + "?activityCode=" + t;
    if(e) {
        n += ("&oraUserId=" + e);
    }
    Suningmatch.isConfig ? Suningmatch.setShare(n) : Suningmatch.wxReady(n)
};

Suningmatch.setShare = function(e) {
    wx.onMenuShareTimeline({
            title: Suningmatch.shareInfo.share_title,
            desc: Suningmatch.shareInfo.share_desc,
            link: e,
            imgUrl: Suningmatch.shareInfo.share_img,
            success: function() {},
            cancel: function() {}
        }),
        wx.onMenuShareAppMessage({
            title: Suningmatch.shareInfo.share_title,
            desc: Suningmatch.shareInfo.share_desc,
            link: e,
            imgUrl: Suningmatch.shareInfo.share_img,
            type: "link",
            dataUrl: "",
            success: function() {},
            cancel: function() {}

        })
};
Suningmatch.getUrlArgs = function() {
    var args = {},
        query = location.search.substring(1),
        pairs = query.split("&"),
        pos = idx = argname = val = null;
    for(var i = 0; i < pairs.length; i++) {
        pos = pairs[i];
        idx = pos.indexOf("=");
        if(idx == -1) continue;
        argname = pos.substring(0, idx);
        val = decodeURIComponent(pos.substring(idx + 1));
        args[argname] = val;
    }
    return args;
};

Suningmatch.sa = function(env) {
    function _loadAsyncJs(src) {
        var _src = src;
        var _scripts = document.getElementsByTagName('script');
        for(var i = 0; i < _scripts.length; i++) {
            if(_scripts[i].src == _src) {
                return;
            }
        }
        var _script = document.createElement('script');
        _script.type = 'text/javascript';
        _script.async = true;
        _script.src = _src;
        var _s = _scripts[0];
        _s.parentNode.insertBefore(_script, _s);
    }
    //创建在页面引入外部js文件的script标签的方法结束

    //判断环境，设置js文件路径开始
    function _getJsFilePath(js_file) {
        var _hostName = document.location.hostname;
        // 一般生产环境的域名
        var _prd_reg = '.suning.com';
        // 一般pre环境的域名
        var _pre_reg = '.cnsuning.com';
        // 一般sit环境的域名
        var _sit_reg = '.cnsuning.com';
        //以上为一般情况下各种环境(sit,pre,prd)的域名格式，如果自己系统环境的域名格式不同，请根据实际情况制定

        var sa_src = '';
        if(sa.env == 'prd' || _hostName.indexOf(_prd_reg) != -1) {
            sa_src = ('https:' == document.location.protocol) ? 'https://imgssl.suning.com' : 'http://script.suning.cn';
        } else if(sa.env == 'pre' || _hostName.indexOf(_pre_reg) != -1) { //sit系统
            sa_src = ('https:' == document.location.protocol) ? 'https://preimgssl.suning.com' : 'http://prescript.suning.cn';
        } else if(sa.env == 'sit' || _hostName.indexOf(_sit_reg) != -1) {
            sa_src = ('https:' == document.location.protocol) ? 'https://sit1imgssl.suning.com' : 'http://sit1script.suning.cn';
        } else {
            sa_src = ('https:' == document.location.protocol) ? 'https://imgssl.suning.com' : 'http://script.suning.cn';
        }
        sa_src = sa_src + '/javascript/sn_da/' + js_file;
        return sa_src;
    }
    //判断环境，设置js文件路径结束

    //调用方法，加载js文件开始
    var sa = sa || {};
    sa.env = 'sit'; //env的取值范围'prd、pre和sit',请根据实际环境配置！上线前请配置prd
    _loadAsyncJs(_getJsFilePath('sa_simple.js'));
    //调用方法，加载js文件结束
};

var passport_config = {
    base: "/zzg-web/",
    loginTheme: "MOBTS_WAP"
};

function cropperImg(){
    //图片裁剪
    var cropper = new Cropper({
        aspectRatio: 'auto',
        element: document.getElementById('cropper-target'),
    //              previews: [
    //                  document.getElementById('preview-large')
    //              ],
        onCroppedRectChange: function(rect) {
            $("#preview-x").val(rect.left);
            $("#preview-y").val(rect.top);
            $("#preview-width").val(rect.width);
            $("#preview-height").val(rect.height);
        }
    });
    var input = document.getElementById('cropper-input');
    input.onchange = function() {
        Suningmatch.isChangeImg = true;
        $("#cropper-target").show();
        $(".modifySrc").hide();
        //$("#cropper-target .noavatar").removeClass("modify");
        if(typeof FileReader !== 'undefined') {
            var reader = new FileReader();
            reader.onload = function(event) {
                cropper.setImage(event.target.result);
                var urlTxt = reader.result;
                setImageURL(urlTxt);
            };
            if(input.files && input.files[0]) {
                reader.readAsDataURL(input.files[0]);
            }
        } else { // IE10-
            input.select();
            input.blur();
            var src = document.selection.createRange().text;
            cropper.setImage(src);
        }
    };
}
Suningmatch.isChangeImg = false;
Suningmatch.cropper = new Image();; 
function setImageURL(urlTxt){
    Suningmatch.cropper.src = urlTxt;
}

Suningmatch.imgprefix = function (val) {
    var res = 0,
    len = val.length;
    if (val.indexOf("/uimg/") >= 0) {
        for (var i = 0; i < len; i++) {
            res = res * 31 + val.charCodeAt(i);
        }
        val = val.replace("?", res % 5 == 0 ? "" : res % 5);
        return val;
    } else {
        return val;
    }
};

$(function() {
    // safari模拟a标签active状态
    var a = document.getElementsByTagName('a');
    for(var i = 0; i < a.length; i++) {
        a[i].addEventListener('touchstart', function() {}, false);
    }

    Suningmatch.sa();
});
